package com.zhixiang.wbl.visitor.Utils;

import android.content.Context;

import com.snappydb.DB;
import com.snappydb.DBFactory;
import com.snappydb.SnappydbException;
import com.zhixiang.wbl.visitor.Constant.DBConstant;
import com.zhixiang.wbl.visitor.model.location.LocationModel;
import com.zhixiang.wbl.visitor.model.login.Visitor;
import com.zhixiang.wbl.visitor.model.token.TokenModel;

/**
 * Created by dongzhixiang on 16/5/19.
 */
public class VisitorUtil
{
   /*
   * 获取当前缓存的用户信息
   * @param Contexts 设备上下文
   * */
   public static Visitor getCurrentVisitor(Context mContext)
   {
      try {
         DB snapDB = DBFactory.open(mContext);
         Visitor visitor = snapDB.get(DBConstant.LoginActivity_VISITOR_KEY,Visitor.class);
         return visitor;
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
      return null;
   }
   /*
   * 缓存当前用户信息
   * @param Visitor 游客对象
   * @param Context 设备上下文
   * */
   public static void cacheVistorInfo(Visitor mVisitor,Context mContext)
   {
      try {
         DB snapDB = DBFactory.open(mContext);
         snapDB.put(DBConstant.LoginActivity_VISITOR_KEY,mVisitor);
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
   }
   /*
   * 缓存当前游客的位置信息
   * */
   public static void cacheLocation(Context mContext, LocationModel location)
   {
      try {
         DB snappydb = DBFactory.open(mContext);
         snappydb.put(DBConstant.Visitor_Location_KEY,location);
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
   }
   /*
   * 从缓存总读取欧克的位置信息
   * */
   public static LocationModel getLocation(Context mContext)
   {
      try {
         DB snapDB = DBFactory.open(mContext);
         LocationModel location = snapDB.get(DBConstant.Visitor_Location_KEY,LocationModel.class);
         return location;
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
      return null;
   }
   /*
   * 缓存token
   * */
   public static void cacheToken(Context mContext, TokenModel token)
   {
      try {
         DB snappydb = DBFactory.open(mContext);
         snappydb.put(DBConstant.LoginActivity_Token_KEY,token);
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
   }
   /*
   * 从缓存中读取token
   * */
   public static TokenModel getToken(Context mContext)
   {
      try {
         DB snapDB = DBFactory.open(mContext);
         TokenModel token = snapDB.get(DBConstant.LoginActivity_Token_KEY,TokenModel.class);
         return token;
      } catch (SnappydbException e) {
         e.printStackTrace();
      }
      return null;
   }
}
